#!/bin/sh

find_self() {
	SELF_DIR=`dirname $0`
}

shmount() {
	su -c "$MOUNT -t $MOUNTTYPE -o rw $SHARED $MOUNTPOINT" -l root
}

shumount() {
	$UMOUNT -l $SHARED
}

create_env() {	
	PLAYPEN="$MOUNTPOINT"
 	#STORAGE="$MOUNTPOINT/storage"
	export PLAYPEN STORAGE
	mkdir -p $PLAYPEN
	#mkdir -p $STORAGE
	cd $PLAYPEN
}

run_application() {

	dos2unix $APP_SCRIPT
	$SH $APP_SCRIPT 1>$APP_STDOUT 2>$APP_STDERR
	echo $? > $APP_EXIT

	unix2dos $APP_STDOUT
	unix2dos $APP_STDERR
	unix2dos $APP_EXIT
}

#MAIN
find_self
. $SELF_DIR/VBOX_OPTS/OG_OPTS
. $SELF_DIR/VBOX_OPTS/CMD_OPTS

CURRENT_DIR=`pwd`

echo "Mounting Shared Folder"
shmount

echo "Create Enviroment"
create_env

dos2unix $PLAYPEN/OG_OPTS
. $PLAYPEN/OG_OPTS

echo "Executing Applications"
run_application

touch $MOUNTPOINT/$TERMINATION_FILE

echo "Unmounting Shared Folder"
shumount
